import { defineStore } from 'pinia'
import request from '@/utils/request'
import {
  ApiRes,
  BannerItem,
  BrandItem,
  CategoryItem,
  HotItem,
  NewItem,
  ProductItem,
  SpecialItem,
} from '@/types/data'
import { topCategory } from '../constants'
const defaultList = topCategory.map((item, index) => {
  return {
    name: item,
  }
})
export default defineStore('home', {
  state: () => {
    return {
      // 原始数据
      categoryList: defaultList as CategoryItem[],
      falg: true,
      // 轮播图数据
      bannerList: [] as BannerItem[],
      // 请求新鲜好物的数据
      newGoodList: [] as NewItem[],
      // 人气推荐数据
      hotGoodList: [] as HotItem[],
      // 热门品牌
      brandGoodList: [] as BrandItem[],
      // 商品区块
      productGoodList: [] as ProductItem[],
      // 最新专题
      specialGoodList:[] as SpecialItem[]
    }
  },
  actions: {
    async getAllCategory() {
      const res = await request.get<ApiRes<CategoryItem[]>>(
        '/home/category/head'
      )
      // console.log(res)
      //获取列表之后要存数据
      this.categoryList = res.data.result
      // console.log(this.categoryList)
    },
    // 二级图片的显示与隐藏
    hide() {
      this.falg = !this.falg
      // console.log(this.falg)
    },
    show() {
      this.falg = true
    },
    // 请求轮播图数据
    async getBannerList() {
      const res = await request.get<ApiRes<BannerItem[]>>('/home/banner')
      // console.log(res)
      this.bannerList = res.data.result
    },
    // 请求新鲜好物的数据
    async getNewList() {
      const res = await request.get<ApiRes<NewItem[]>>('/home/new')
      // console.log(res);
      this.newGoodList = res.data.result
    },
    // 人气推荐
    async getGoodList() {
      const res = await request.get<ApiRes<HotItem[]>>('/home/hot')
      console.log(res)
      this.hotGoodList = res.data.result
    },
    //热门品牌
    async getBrandList() {
      const res = await request.get<ApiRes<BrandItem[]>>('/home/brand')
      // console.log(res)
      this.brandGoodList = res.data.result
    },
    // 商品区块
    async getProductList() {
      const res = await request.get<ApiRes<ProductItem[]>>('/home/goods')
      // console.log(res)
      this.productGoodList = res.data.result
    },
    // 最新专题
    async getSpecialList(){
      const res = await request.get<ApiRes<SpecialItem[]>>('/home/special')
      // console.log(res);
      this.specialGoodList=res.data.result
    }
  },
})
